// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'

import Mint from'mint-ui'
import'mint-ui/lib/style.css'
import { Toast } from 'mint-ui'

Vue.use(Mint);
Vue.prototype.$http = axios;
// 异常处理

var firstTime = true;
function addInterceptor(){
  axios.interceptors.response.use(response => {
    /*return {
      code: response.status,
      message: response.statusText,
      data: response.data
    }*/
    return response.data
  }, err => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = '错误请求'
          break
        case 401:
          if(!firstTime){
            err.message = '未授权，请重新登录'
            router.push({path:"/login/login"});
          }
          break
        case 403:
          err.message = '拒绝访问'
          break
        case 404:
          err.message = '请求错误,未找到该资源'
          break
        case 405:
          err.message = '请求方法未允许'
          break
        case 408:
          err.message = '请求超时'
          break
        case 500:
          err.message = '服务器端出错'
          break
        case 501:
          err.message = '网络未实现'
          break
        case 502:
          err.message = '网络错误'
          break
        case 503:
          err.message = '服务不可用'
          break
        case 504:
          err.message = '网络超时'
          break
        case 505:
          err.message = 'http版本不支持该请求'
          break
        default:
          err.message = `连接错误${err.response.status}`
      }
      let errData = {
        code: err.response.status,
        message: err.message
      }
      // 统一错误处理可以放这，例如页面提示错误...
      console.log('统一错误处理: ', errData)
    }
    return Promise.reject(err)
  })
}

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  data: function () {
    return {
      year:"2019",
      userPromiss:"",
      userInfo:{},
      batchDate:[],
      videoServerUrl:[]
    }
  },
  created(){
    let vm = this;
    addInterceptor();
    this.userPromiss = new Promise(function(resolve, reject){
        vm.$http.get('/auth/userInfo').then(resp=>{
          vm.userInfo = resp.data.userInfo;
          resp.data.allDict.forEach(dict=>vm[dict.type]=eval(dict.jsonStr));
          vm.checkNetwork();
          firstTime = false;
          resolve(vm.userInfo)
        }).catch((err)=>{
          firstTime = false;
          resolve(undefined)
        });
    });
  },
  router,
  components: { App },
  template: '<App/>'
})
